package com.yyc.stocktake.ui.frg

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LanguageUtils
import com.blankj.utilcode.util.LogUtils
import com.yyc.stocktake.R
import com.yyc.stocktake.api.ApiService
import com.yyc.stocktake.base.BaseFragment
import com.yyc.stocktake.bean.AppRoomDataBase
import com.yyc.stocktake.bean.db.ConfigBean
import com.yyc.stocktake.databinding.FSettingBinding
import com.yyc.stocktake.ext.showMessage
import com.yyc.stocktake.ext.showToast
import com.yyc.stocktake.util.CacheUtil
import com.yyc.stocktake.viewmodel.SettingModel
import com.yyc.stocktake.weight.PopupWindowTool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.ext.nav
import java.util.Locale

/**
 * @Author nike
 * @Date 2023/7/6 18:02
 * @Description  设置
 */
class SettingFrg: BaseFragment<SettingModel, FSettingBinding>() {

    var languagePosition: Int = 0

    var languageChoosePosition: Int = 0

    val configDao = AppRoomDataBase.get().getConfigDao()

    val languageOptions = arrayOf(
        "简体中文",
        "繁體中文",
        "English"
    )

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        val languageAdapter = ArrayAdapter(requireContext(), R.layout.i_language, languageOptions)
        mDatabind.spLanguage.adapter = languageAdapter

        mViewModel.viewModelScope.launch(Dispatchers.IO) {
            var configBean = configDao.findFirst()
            if (configBean.language.isEmpty()){
                var locale = LanguageUtils.getSystemLanguage().displayScript
                languagePosition = languageOptions.indexOf(locale)
                if (languagePosition == -1){
                    languagePosition = languageOptions.size - 1
                }
            }else{
                languagePosition = languageOptions.indexOf(configBean.language)
            }
            mViewModel.host.set(configBean.httpUrl)
            mViewModel.companyId.set(configBean.companyId)
            withContext(Dispatchers.Main) {
                languageChoosePosition = languagePosition
                mDatabind.spLanguage.setSelection(languageChoosePosition)
                // 手动触发 onItemSelected 方法
                if (languageChoosePosition == 0){
                    mDatabind.spLanguage.onItemSelectedListener?.onItemSelected(
                        mDatabind.spLanguage,
                        mDatabind.spLanguage.selectedView,
                        0,
                        mDatabind.spLanguage.selectedItemId
                    )
                }
            }
        }

        mDatabind.spLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                // 当选择项发生变化时触发的逻辑
                languageChoosePosition = position
                if (view != null){
                    (view as TextView).setTextColor(requireActivity().getColor(R.color.white))
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 当没有选择项时触发的逻辑
            }
        }
    }

    inner class ProxyClick(){

        fun close(){
            nav().navigateUp()
        }

        fun setlanguage(){
            /*PopupWindowTool.showListDialog(activity)
                .asCenterList(getString(R.string.please_language),
                    languageOptions,{ position, text ->
                        mViewModel.language.set(languageOptions[position])
                        languagePosition = position
                    }).show()*/
        }

        fun setSave(){
            mViewModel.viewModelScope.launch(Dispatchers.IO) {
                var configBean = configDao.findFirst()
                if (languageChoosePosition != languagePosition) {
                    LanguageUtils.applyLanguage(
                        when (languageChoosePosition) {
                        0 -> Locale.SIMPLIFIED_CHINESE
                        1 -> Locale.TRADITIONAL_CHINESE
                        2 -> Locale.ENGLISH
                        else -> Locale.ENGLISH
                    })
                    configBean.language = languageOptions[languageChoosePosition]
                }
                if ((mViewModel.host.get().isEmpty() && mViewModel.companyId.get().isEmpty())) {
                    showMessage(message = getString(R.string.hiht_))
                    return@launch
                }
                configBean.httpUrl = mViewModel.host.get()
                configBean.companyId = mViewModel.companyId.get()
                configDao.update(configBean)
                CacheUtil.setCompanyID(configBean.companyId)
                CacheUtil.setUrl(configBean.httpUrl)
                showToast(getString(R.string.release_success))
            }
        }
    }

}